<template> 
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
        <div>
          <i class="el-icon-search"></i>
          <span>筛选搜索</span>
          <el-button
            style="float: right"
            @click="searchBrandList()"
            type="primary"
            size="small">
            查询结果
          </el-button>
           <el-button
          style="float:right;margin-right: 15px"
          @click="handleResetSearch()"
          size="small">
          重置
        </el-button>
        </div>
        <div style="margin-top: 15px">
          <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
            <el-form-item label="输入搜索：">
              <el-input style="width: 203px" v-model="listQuery.bname" placeholder="品牌名称/关键字"></el-input>
            </el-form-item>
          </el-form>
        </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>

      <span>数据列表</span>

      <el-button
        class="btn-add"
        @click="addBrand()"
        size="mini">
        添加
      </el-button>

      <el-button
        class="btn-add"
        :disabled="multipleSelection.length<1"
        size="mini"
        type="danger"
        @click="multiDelete()">
              批量删除
      </el-button>

    </el-card>
    <div class="table-container">
      <el-table ref="brandTable"
                :data="list"
                style="width: 100%"
                @selection-change="handleSelectionChange"
                border>
        <el-table-column type="selection" width="60" align="center"></el-table-column>
        <el-table-column label="编号" width="100" align="center">
          <template slot-scope="scope">{{scope.row.bid}}</template>
        </el-table-column>
        <el-table-column label="品牌名称" align="center">
          <template slot-scope="scope">{{scope.row.bname}}</template>
        </el-table-column>
        <!-- <el-table-column label="品牌首字母" width="100" align="center">
          <template slot-scope="scope">{{scope.row.firstLetter}}</template>
        </el-table-column>
        <el-table-column label="排序" width="100" align="center">
          <template slot-scope="scope">{{scope.row.sort}}</template>
        </el-table-column> -->
       
     

        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleUpdate(scope.row)">编辑
            </el-button>

            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
 <el-dialog
      :title="isEdit?'修改品牌':'添加品牌'"
      :visible.sync="dialogVisible"
      width="40%">
      <el-form :model="brand"
               ref="brandForm"
               label-width="150px" size="small">
        <el-form-item label="品牌">
          <el-input v-model="brand.bname" style="max-width: 300px;"></el-input>
        </el-form-item>
       
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="handleDialogConfirm()" size="small">确 定</el-button>
      </span>
    </el-dialog>
    
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pagesize"
        :page-sizes="[10,20,50]"
        :current-page.sync="listQuery.page"
        :total="total">
      </el-pagination>
    </div>
  </div>
</template>
<script>
  // import {fetchList, updateShowStatus, updateFactoryStatus, deleteBrand} from '@/api/brand'
  import {fetchList, createBrand, updateBrand, deleteBrand} from '@/api/brand'
  const defaultListQuery = {
    page: 1,
    pagesize: 10,
    bname: null
  };
  const defualtBrand ={
    bname:null,
    bid:null
  }
  export default {
    name: 'brandList',
    data() {
      return {
        isEdit:false,
        dialogVisible:false,
        listQuery:Object.assign({}, defaultListQuery),
        brand:Object.assign({},defualtBrand),
        list: null,
        total: null,
        multipleSelection:[]
       
      }
    },
    created() {
      this.getList();
    },
    methods: {
       handleSelectionChange(val) {
        this.multipleSelection = val;
        
      },
      // 查询列表
      getList() {
        fetchList(this.listQuery).then(response => {
          this.list = response.obj.list;
          this.total = response.obj.count;
        });
      },
      handleResetSearch() {
        this.listQuery = Object.assign({}, defaultListQuery);
      },
      handleUpdate(row) {
        this.dialogVisible = true;
        this.isEdit = true;
        this.brand = Object.assign({},row);
      },
      multiDelete(){
        console.log(this.multipleSelection);
        let list = this.multipleSelection;
        let data=list.map((res)=>{
          return res.bid
        })
        this.$confirm(`是否批量删除选中的${data.length}个品牌`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteBrand({array:data}).then(response => {
            this.$message({
              message: '删除成功',
              type: 'success',
              duration: 1000
            });
            this.getList();
          });
        });

       
      },
      
      handleDelete(index, row) {
        this.$confirm('是否要删除该品牌', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteBrand({array:[row.bid]}).then(response => {
            this.$message({
              message: '删除成功',
              type: 'success',
              duration: 1000
            });
            this.getList();
          });
        });
      },
       // 新增or编辑
      handleDialogConfirm() {
        this.$confirm('是否要确认?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          
          if (this.isEdit) {
            if(this.brand.bname===''){ 
                this.$message({
                  type: 'error',
                  message: '不能为空!'
                })
                return
            }
            updateBrand({bid:this.brand.bid,bname:this.brand.bname}).then(response => {
              this.$message({
                message: response.msg,
                type: 'success'
              });
              this.dialogVisible =false;
              this.getList();
            })
          } else {
            createBrand({bname:this.brand.bname}).then(response => {
              this.$message({
                message: response.msg,
                type: 'success'
              });
              this.dialogVisible =false;
              this.getList();
            })
          }
        })
      },
 
      handleSizeChange(val) {
        this.listQuery.page = 1;
        this.listQuery.pagesize = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.page = val;
        this.getList();
      },
      searchBrandList() {
        this.listQuery.page = 1;
        this.getList();
      },
     
      addBrand() {
        this.dialogVisible = true;
        this.isEdit = false;
        this.brand = Object.assign({},defualtBrand);
      }
    }
  }
</script>
<style rel="stylesheet/scss" lang="scss" scoped>


</style>


